// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

const kButtonColors = [
  '',
  'rgb(227, 227, 227)', 
  'rgb(208, 208, 208)',
  'rgb(44, 62, 80)',
  'rgb(38, 166,91)',
  'rgb(132, 175, 155)',
  'rgb(174, 221, 129)',
  'rgb(210, 77, 87)',
  'rgb(203,153,197)',
  'rgb(235, 115, 71)',
  'rgb(252, 157, 153)',
  'rgb(249, 187, 45)', 
  'rgb(70, 136, 241)',
  'rgb(102,152,203)',
  'rgb(0, 204, 255)',
  'rgb(127,204,222)'
];


/** 1. usr page background cofig */
let page = document.getElementById('buttonDiv');

function updateColorBtnStyle(cfgColor) {
  for (let item of page.children) {
    if (cfgColor === item.style.backgroundColor) {
      item.className = "color_option";
    } else {
      item.className = "";
    }
  }
  page.children[0].style.verticalAlign = "bottom";
  page.children[0].textContent = "无";
}

function saveSetting(item) {
    chrome.storage.sync.set({bgcolor: item}, function() {
      updateColorBtnStyle(item);
      console.log('color is set to' + item);
    })
}

function getAutoEnableCfg() {
  chrome.storage.sync.set({i_csdn:i_csdn.checked}, function() {
    console.log('iread csdn is set to ' + i_csdn.checked);
  });
  chrome.storage.sync.set({i_doc360:i_doc360.checked}, function() {
    console.log('iread doc360 is set to ' + i_doc360.checked);
  });
  chrome.storage.sync.set({i_jb51:i_jb51.checked}, function() {
    console.log('iread jb51 is set to ' + i_jb51.checked);
  });
  chrome.storage.sync.set({i_sohu:i_sohu.checked}, function() {
    console.log('iread sohu is set to ' + i_sohu.checked);
  });
  chrome.storage.sync.set({i_jianshu:i_jianshu.checked}, function() {
    console.log('iread jianshu is set to ' + i_jianshu.checked);
  });
}

function constructColorOptions(kButtonColors, cfgColor) {
  for (let item of kButtonColors) {
    let button = document.createElement('button');
    button.style.background = item;
    button.addEventListener('click', ()=>saveSetting(item));
    page.appendChild(button);
  }
  updateColorBtnStyle(cfgColor);
}

chrome.storage.sync.get(['bgcolor'], function(result) {
  console.log('option->bgcolor is ' + result.bgcolor);
  constructColorOptions(kButtonColors, result.bgcolor);
});

/** 2. iread auto cofig */
let i_csdn   = document.getElementById("iread_csdn");
let i_doc360 = document.getElementById("iread_360doc");
let i_jb51   = document.getElementById("iread_jb51");
let i_sohu   = document.getElementById("iread_sohu");
let i_jianshu= document.getElementById("iread_jianshu");

i_csdn.addEventListener("click", getAutoEnableCfg);
i_doc360.addEventListener("click", getAutoEnableCfg);
i_jb51.addEventListener("click", getAutoEnableCfg);
i_sohu.addEventListener("click", getAutoEnableCfg);
i_jianshu.addEventListener("click", getAutoEnableCfg);


chrome.storage.sync.get(['i_csdn'], function(result) {
  i_csdn.checked = result.i_csdn;
});
chrome.storage.sync.get(['i_doc360'], function(result) {
  i_doc360.checked = result.i_doc360;
});
chrome.storage.sync.get(['i_jb51'], function(result) {
  i_jb51.checked = result.i_jb51;
});
chrome.storage.sync.get(['i_sohu'], function(result) {
  i_sohu.checked = result.i_sohu;
});
chrome.storage.sync.get(['i_jianshu'], function(result) {
  i_jianshu.checked = result.i_jianshu;
});

/** 3. usr page enable cofig */

let csdn   = document.getElementById("web_csdn");
let wenku  = document.getElementById("web_wenku");
let doc360 = document.getElementById("web_360doc");
let jb51   = document.getElementById("web_jb51");
let sohu   = document.getElementById("web_sohu");
let jianshu= document.getElementById("web_jianshu");
let donate = document.getElementById("show_donate");
let baidusearch = document.getElementById("web_baidu");

csdn.addEventListener("click", getWebSiteEnableCfg);
wenku.addEventListener("click", getWebSiteEnableCfg);
doc360.addEventListener("click", getWebSiteEnableCfg);
jb51.addEventListener("click", getWebSiteEnableCfg);
sohu.addEventListener("click", getWebSiteEnableCfg);
jianshu.addEventListener("click", getWebSiteEnableCfg);
donate.addEventListener("click", getWebSiteEnableCfg);
baidusearch.addEventListener("click", getWebSiteEnableCfg);

chrome.storage.sync.get(['csdn'], function(result) {
  csdn.checked = result.csdn;
});
chrome.storage.sync.get(['jb51'], function(result) {
  jb51.checked = result.jb51;
});
chrome.storage.sync.get(['doc360'], function(result) {
  doc360.checked = result.doc360;
});
chrome.storage.sync.get(['sohu'], function(result) {
  sohu.checked = result.sohu;
});
chrome.storage.sync.get(['jianshu'], function(result) {
  jianshu.checked = result.jianshu;
});
chrome.storage.sync.get(['donate'], function(result) {
  donate.checked = result.donate;
});
chrome.storage.sync.get(['baidusearch'], function(result) {
  baidusearch.checked = result.baidusearch;
});
chrome.storage.sync.get(['wenku'], function(result) {
  wenku.checked = result.wenku;
});

function getWebSiteEnableCfg() {
  chrome.storage.sync.set({csdn:csdn.checked}, function() {
    console.log('csdn is set to ' + csdn.checked);
  });
  chrome.storage.sync.set({wenku:wenku.checked}, function() {
    console.log('wenku is set to ' + wenku.checked);
  });
  chrome.storage.sync.set({jb51:jb51.checked}, function() {
    console.log('jb51 is set to ' + jb51.checked);
  });
  chrome.storage.sync.set({doc360:doc360.checked}, function() {
    console.log('360doc is set to ' + doc360.checked);
  });
  chrome.storage.sync.set({sohu:sohu.checked}, function() {
    console.log('sohu is set to ' + sohu.checked);
  });
  chrome.storage.sync.set({jianshu:jianshu.checked}, function() {
    console.log('jianshu is set to ' + jianshu.checked);
  });
  chrome.storage.sync.set({baidusearch:baidusearch.checked}, function() {
    console.log('baidu is set to ' + baidusearch.checked);
  });
  chrome.storage.sync.set({donate:donate.checked}, function() {
    console.log('donate is set to ' + donate.checked);
  });
}
